package cn.bjxy.javaks.store_ms.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.bjxy.javaks.store_ms.entity.GoodsEntity;
import cn.bjxy.javaks.store_ms.ui.MainFrame;
import cn.bjxy.javaks.store_ms.ui.MyFrame;
import cn.bjxy.javaks.store_ms.utils.StoreMSysUtil;

/**
 * 界面货物信息管理业务处理代码
 * 
 * @author Administrator
 *
 */
public class UIGoddsService {
	public static IGoodsService goodsService = GoodsServiceImpl.getProxyInstance();

	/**
	 * 获得所有货物信息
	 * 
	 * @flag 调用标识 true超级管理员界面调用 false 普通管理员界面调用
	 */
	public static void getAllGoods(boolean flag) {
		/* 获得所有货物实体 */
		List goodsList = goodsService.findAll();
		if (flag) {
			for (int i = 0; i < goodsList.size(); i++) {
				/* 走超级管理员界面 */
				MainFrame.addRow(createRowData((GoodsEntity) goodsList.get(i)));
			}
		} else {
			for (int i = 0; i < goodsList.size(); i++) {
				/* 走普通管理员界面 */
				MyFrame.addRow(createRowData((GoodsEntity) goodsList.get(i)));
			}
		}
	}

	/**
	 * 判断货物信息是否添加
	 * 
	 * @return
	 */
	public static boolean goodsIsAdd(boolean frameFlag) {
		GoodsEntity goodsEntity = createEntity(true, frameFlag);
		if (frameFlag) {
			if (goodsService.add(goodsEntity) == 1) {
				/* 走超级管理员界面 */
				MainFrame.addRow(createRowData(goodsService.findById(goodsEntity.getId())));
				return true;
			} else {
				return false;
			}
		} else {
			if (goodsService.add(goodsEntity) == 1) {
				/* 走普通管理员界面 */
				MyFrame.addRow(createRowData(goodsService.findById(goodsEntity.getId())));
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 * 判断货物信息是否被删除
	 * 
	 * @param id
	 *            要删除货物的ID
	 * @return
	 */
	public static boolean goodsIsDelete(String id) {
		return (goodsService.delete(id) == 1 ? true : false);
	}

	/**
	 * 判断货物信息是否修改
	 * 
	 * @param 调用标识
	 *            true超级管理员界面调用 false 普通管理员界面调用
	 * @return
	 */
	public static boolean goodsIsModify(boolean frameFlag) {
		GoodsEntity goodsEntity = createEntity(false, frameFlag);
		if (frameFlag) {
			if (goodsService.modify(goodsEntity) == 1) {
				/* 走超级管理员界面 */
				MainFrame.modifyRow(createRowData(goodsService.findById(goodsEntity.getId())));
				return true;
			} else {
				return false;
			}
		} else {
			if (goodsService.modify(goodsEntity) == 1) {
				/* 走普通管理员界面 */
				MyFrame.modifyRow(createRowData(goodsService.findById(goodsEntity.getId())));
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 * * 封装货物实体
	 * 
	 * @param flag
	 *            true 添加 需要获取新ID false 修改 不需要新ID
	 * @param frameFlag
	 *            调用标识 true 超级管理员界面调用 false 普通管理员界面调用
	 * @return
	 */
	private static GoodsEntity createEntity(boolean flag, boolean frameFlag) {
		GoodsEntity entity = new GoodsEntity();
		String[] rowData = (frameFlag ? MainFrame.getData() : MyFrame.getData());

		if (flag)
			entity.setId(StoreMSysUtil.generateShortUuid());
		else
			entity.setId(rowData[0]);
		entity.setName(rowData[1]);
		entity.setAll_num(Integer.valueOf(rowData[2]));
		entity.setCurrent_num(Integer.valueOf(rowData[3]));
		entity.setBuyer_name(rowData[4]);
		entity.setDescribe(rowData[5]);
		try {
			SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date addDate = form.parse("2008-08-08 12:10:12");
			Date buyDate = form.parse("2009-01-01 13:13:13");
			Date expireDate = form.parse("2018-06-07 20:00:00");
			entity.setAdd_date(addDate);
			entity.setBuy_date(buyDate);
			entity.setExpire_date(expireDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return entity;
	}

	/**
	 * 封装表格管理员信息
	 * 
	 */
	private static String[] createRowData(GoodsEntity goodsEntity) {

		String id = goodsEntity.getId();
		String name = goodsEntity.getName();
		String allNum = String.valueOf(goodsEntity.getAll_num());
		String currentNum = String.valueOf(goodsEntity.getCurrent_num());
		String buyerName = goodsEntity.getBuyer_name();
		String describe = goodsEntity.getDescribe();
		String addDate = String.valueOf(goodsEntity.getAdd_date());
		String buyData = String.valueOf(goodsEntity.getBuy_date());
		String expireDate = String.valueOf(goodsEntity.getExpire_date());
		String modefyDate = String.valueOf(goodsEntity.getModify_date());

		String[] rowData = { id, name, allNum, currentNum, buyerName, describe, addDate, buyData, expireDate,
				modefyDate };
		return rowData;
	}
}
